package com.phone580.app.updateapp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.phone580.app.JavascriptProxy;
import com.phone580.app.R;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.entity.UpgradeEntity;

public class UpdateAppAdapter extends BaseAdapter{
	private LayoutInflater inflater;
	private List<UpgradeEntity> data;
	private Context context;
	
	public UpdateAppAdapter(Context context) {
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
	}

	public void setData(List<UpgradeEntity> data) {
		this.data = data;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public UpgradeEntity getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.update_app_item, null);
			holder = new ViewHolder();
			holder.ivIcon = (ImageView) v.findViewById(R.id.update_app_icon_iv);
			holder.tvTile = (TextView) v.findViewById(R.id.update_app_title_tv);
			holder.tvVersion = (TextView) v.findViewById(R.id.update_app_version_tv);
			holder.btnDownload = (Button) v.findViewById(R.id.update_app_download_btn);
			v.setTag(holder);
		}
		holder = (ViewHolder) v.getTag();
		final UpgradeEntity entity = data.get(position);
		Drawable icon = getAppIcon(entity.getPkgName());
		if (icon != null) {
			holder.ivIcon.setImageDrawable(icon);
		} else {
			holder.ivIcon.setImageResource(R.drawable.ic_launcher);
		}
		holder.tvTile.setText(entity.getAppName());
		holder.tvVersion.setText("最新版本：" + entity.getVersion());
		holder.btnDownload.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Uri packageURI = Uri.parse("package:" + entity.getPkgName()); 
//			    Intent intent = new Intent(Intent.ACTION_DELETE); 
//			    intent.setData(packageURI);
//			    ((Activity)context).startActivityForResult(intent, 1);
				if ("".equals(entity.getAppURL()) ||"".equals(entity.getIconURL())) {
					return;
				}
				JavascriptProxy proxy = new JavascriptProxy(context);
				proxy.setNotice("更新已添加至下载列表，可在下载管理器中查看");
				proxy.download(entity.getAppName(), entity.getIconURL(), entity.getAppURL());
			}
		});
		
		return v;
	}
	
	private Drawable getAppIcon(String pkgName) {
		PackageManager pkg = context.getPackageManager();
		try {
			Drawable drawable = pkg. getApplicationIcon(pkgName);
			return drawable;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	class ViewHolder {
		public ImageView ivIcon;
		public TextView tvTile;
		public TextView tvVersion;
		public Button btnDownload;
	}

}
